/**

 * Copyright 2014-2015 www.fsfkj.com
 * All rights reserved.
 * 
 * @project
 * @author Flouny.Caesar
 * @version 1.0
 * @date 2015-10-20
 */
package com.eyaomall.wxmanage.common.initial;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eyaomall.wxmanage.common.util.EhCacheUtil;
import com.eyaomall.wxmanage.model.po.Resource;
import com.eyaomall.wxmanage.service.ResourceService;

/**
 * 
 * @author Flouny.Caesar
 *
 */
@Service
public class ResourceInitial implements BeanFactoryAware {
	
	@Autowired
	private ResourceService resourceService;
	
	@Override
	
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		
		List<Resource> resources = resourceService.getResources();
		
		Map<Integer, Resource> resourceMap = new HashMap<Integer, Resource>();
		for (Resource resource: resources) {
			resourceMap.put(resource.getId(), resource);
		}
		
		
		EhCacheUtil.put("resourceMap", resourceMap);
		
		EhCacheUtil.put("resourceList", resources);
		
		
	}
	/**
	 * 重新加载缓存
	 */
	public void reload(){
	List<Resource> resources = resourceService.getResources();
		
		Map<Integer, Resource> resourceMap = new HashMap<Integer, Resource>();
		for (Resource resource: resources) {
			resourceMap.put(resource.getId(), resource);
		}
		
		
		EhCacheUtil.put("resourceMap", resourceMap);
		
		EhCacheUtil.put("resourceList", resources);
		
	}
}